/**
 * Foxit Software Inc.
 * Copyright(C) 2005-2014, all rights reserved.
 * The following code is copyrighted and contains proprietary information
 * and trade secrets of Foxit Software Inc.

 * Description: PDFPage
 * Author: shaojie zhuo
 * Update: shaojie zhuo (2014-05-07 17:57)
 */

define(function (require, exports, module) {
    var ImageEngine = require('./ImageEngine.js');
    var PDFMatrix = WebPDF.PDFMatrix;
    /**
     * enum for key of page data in JSON format.
     * @enum
     */
    ImageEngine.PageDataJsonFormat = {
        PAGE_WIDTH: "width",
        PAGE_HEIGHT: "height",
        PAGE_ROTATE: "rotate",
        PAGE_INDEX: "number",
        PAGE_MATRIX: "matrix"
    };

    /**
     * Create a PDF page object by data in JSON format.
     * @param {Object} pageJSONData
     * @constructor
     */
    var PDFPage = Backbone.Model.extend({   

        initialize: function(){
             var pdfDataJsonFormat = ImageEngine.PageDataJsonFormat;       
             this.pageIndex = this.get(pdfDataJsonFormat.PAGE_INDEX);
             this.pageWidth = this.get(pdfDataJsonFormat.PAGE_WIDTH);
             this.pageHeight = this.get(pdfDataJsonFormat.PAGE_HEIGHT);
             this.rotate = this.get(pdfDataJsonFormat.PAGE_ROTATE);
             this.pageMatrix = new PDFMatrix();
             var jsonMatrix = this.get(pdfDataJsonFormat.PAGE_MATRIX);
             if(jsonMatrix)
                this.pageMatrix.Set(jsonMatrix[0],jsonMatrix[1],jsonMatrix[2],jsonMatrix[3],jsonMatrix[4],jsonMatrix[5]);
             else
                this.pageMatrix.Set(1,0,0,1,0,0);
            
            this.annotationsMap = {};
            this.isModifyed = false;
        },

        /**
         * Get index of page.
         * @returns {number} index of page
         */
        getPageIndex : function() {
            return this.pageIndex;
        },
        /**
         * Get width of page in pt.
         * @returns {number} width of page in pt.
         */
        getPageWidth : function(){
            return this.pageWidth;
        },
        /**
         * Get height of page in pt.
         * @returns {number} height of page in pt.
         */
        getPageHeight : function(){
            return this.pageHeight;
        },
       
        /**
         * Get rotate of page.
         * @returns {number} rotate of page.
         */
        getPageRotate : function () {
            return this.rotate;
        },
       
        /**
         * Return matrix of page.
         * @returns {WebPDF.PDFMatrix}
         */
        getPageMatrix : function(){
            return this.pageMatrix;
        },

        //extra functions for Image Engine

        /**
         * Get annotation in map.
         * @param annotName
         * @param annotMap
         * @returns {*}
         * @private
         */
        getAnnotInMap : function(annotName, annotMap) {
            if (annotMap == null)
                return null;
            return annotMap[annotName];
        },

        getAnnotByName : function (annotName) {            
            return this.getAnnotInMap(annotName, this.annotationsMap);
        },

        enumAnnots : function(callback){
            for ( var key in this.annotationsMap ) {
                if (callback.call(this, this.annotationsMap[ key ]) === false ) {
                    break;
                }
            }
        },

        /**
         * 批量删除annotation
         * @param  {Object} annotMapToDelete 需要删除的annotation列表，格式为 {annotName : 1}
         * @private
         */
        deleteAnnots : function (annotMapToDelete) {
            for (var j = 0; j < this.annotJsonData.length; j++) {
                var jsonAnnot = this.annotJsonData[j];
                var name = jsonAnnot[WebPDF.PDFData.AnnotDataJSONFormat.name];
                if (annotMapToDelete[name]) {
                    this.annotJsonData.splice(j, 1);
                    j = j - 1;
                    delete this.annotationsMap[name];
                }
            }
        },


        addAnnot : function (annot) {
            return this.annotationsMap[annot.getAnnotName()] = annot;
        },

        getAnnotCount : function () {            
            return this.annotationsMap.size();
        },

        getAnnotJsonData : function () {
            return this.annotJsonData;
        },

        setAnnotJsonData : function (annotJsonData) {
            this.annotJsonData = annotJsonData;
        },
        
        
        isModified : function () {
            return this.isModifyed;
        },

        setModified : function (isModifyed) {
            this.isModifyed = isModifyed;
        },
    });
    
    module.exports = PDFPage;
});


